package org.wedding.center.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.minio.errors.*;
import org.wedding.center.util.QiniuUtils;
import org.wedding.dto.PageModel;
import org.wedding.center.dto.query.AttachQuery;
import org.wedding.center.entity.AttachInfo;
import org.wedding.center.service.AttachInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.wedding.system.entity.UserInfo;
import org.wedding.utils.AuthUtils;
import org.wedding.utils.CommonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 图片附件表(AttachInfo)表控制层
 *
 * @author songkaiyue
 * @since 2025-05-14 16:30:26
 */
@AllArgsConstructor
@RestController
@RequestMapping("/attachInfo")
@Tag(name = "AttachInfo", description = "图片附件表")
public class AttachInfoController{
    private final AttachInfoService attachInfoService;
    @Resource
    private QiniuUtils qiniuUtils;

    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @SaIgnore
    @Operation(summary = "分页查询所有数据")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> list(AttachQuery query) {
        return attachInfoService.findAll(query);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @RequestMapping(value = "/view", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult view(String id) {
        return attachInfoService.view(id);
    }

    /**
     * 新增数据
     *
     * @param attachInfo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult add(@RequestBody AttachInfo attachInfo) {
        return attachInfoService.add(attachInfo);
    }

    /**
     * 修改数据
     *
     * @param attachInfo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改数据")
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult update(@RequestBody AttachInfo attachInfo) {
        return attachInfoService.updateEntity(attachInfo);
    }

    /**
     * 修改文件名
     *
     * @param id
     * @param fileName
     * @return
     */
    @Operation(summary = "修改文件名")
    @RequestMapping(value = "/updateFileName", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult updateFileName(String id, String fileName) {
        return attachInfoService.updateFileName(id, fileName);
    }

   /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除结果
     */
    @Operation(summary = "删除数据")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult delete(@RequestBody List<String> ids) {
        return attachInfoService.delete(ids);
    }

    /**
     * 根据sort和businessId查询
     * @param sort
     * @param businessId
     * @return
     */
    @Operation(summary = "根据sort和businessId查询")
    @RequestMapping(value = "/findBySortAndBusinessId", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult findBySortAndBusinessId(String sort, String businessId) {
        return attachInfoService.findBySortAndBusinessId(sort, businessId);
    }


    @Operation(summary = "上传图片文件")
    @PostMapping(value = "/uploadAttach")
    public CommonResult<AttachInfo> uploadAttach(@RequestBody AttachInfo attachInfo) {
        return attachInfoService.uploadAttach(attachInfo);
    }

    @Operation(summary = "上传图片")
    @PostMapping(value = "/upload")
    public CommonResult upload(@RequestParam("file") MultipartFile file) {
        try{
            FileInputStream uploadFile = (FileInputStream) file.getInputStream();
            String upload = qiniuUtils.upload(uploadFile, file.getOriginalFilename());
            return CommonResult.success(upload);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
}

